<template>
  <div>
    <p>
      <button v-on:click="list()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>

    <div class="row">
      <div class="col-md-6">
        <ul id="tree" class="ztree"></ul>
      </div>
    </div>
  </div>
</template>

<script>
import Toast from "../../../public/static/js/toast";
import Loading from "../../../public/static/js/loading";
import Confirm from "../../../public/static/js/confirm";

export default {
  name: "system-resource",
  data: function () {
    return {
      resource: {},
      resources: [],
      resourceStr: "",
      tree: {},
      base: `${process.env.VUE_APP_BASE_URL}/system`,
    };
  },
  mounted: function () {
    let _this = this;
    _this.list();
    // sidebar激活样式方法一
    // this.$parent.activeSidebar("system-resource-sidebar");
  },
  methods: {
    /**
     * 列表查询
     */
    list() {
      let _this = this;
      Loading.show();
      _this.$axios.get(`${_this.base}/admin/resources/loadTree`).then((res) => {
        Loading.hide();
        _this.resources = res.data.data;
        // 初始化树
        _this.initTree();
      });
    },

    /**
     * 点击【删除】
     */
    del(id) {
      let _this = this;
      Confirm.show("删除资源后不可恢复，确认删除？", function () {
        Loading.show();
        _this.$axios
          .delete(`${_this.base}/system/admin/resources/${id}`)
          .then((res) => {
            Loading.hide();
            if (res.data.success) {
              _this.list(1);
              Toast.success("删除成功！");
            } else {
                Toast.error(res.data.msg)
            }
          });
      });
    },

    /**
     * 初始资源树
     */
    initTree() {
      let _this = this;
      let setting = {
        data: {
          simpleData: {
            idKey: "id",
            pIdKey: "parent",
            rootPId: "",
            // enable: true
          },
        },
      };

      _this.zTree = $.fn.zTree.init($("#tree"), setting, _this.resources);
      _this.zTree.expandAll(true);
    },
  },
};
</script>